package com.mc.read.adminapi.manager;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mc.read.adminapi.valid.OssValidVo;
import com.mc.read.common.MapConfig;
import com.mc.read.common.emun.ServiceCodeEnum;
import com.mc.read.common.exception.ServiceException;
import com.mc.read.common.util.Md5Util;
import com.mc.read.common.util.TreeUtils;
import com.mc.read.common.util.ValidParamUtils;
import com.mc.read.dao.mapper.sys.*;
import com.mc.read.dao.models.sys.*;
import com.mc.read.dao.vo.PageResposeVo;
import com.mc.read.dao.vo.PageVo;
import com.mc.read.dao.vo.TreeNode;
import com.mc.read.vo.request.BaseRequest;
import com.mc.read.vo.respose.BaseRespose;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author liuhao
 * @date 2019/6/13 11:43
 */
@Component
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
public class UserManager {
    @Autowired
    SysuserMapper sysuserMapper;
    @Autowired
    OssValidVo ossValidVo;
    @Autowired
    CacheManager cacheService;
    @Autowired
    SysroleMapper sysroleMapper;
    @Autowired
    SysuserroleMapper sysuserroleMapper;
    @Autowired
    SyspermissionMapper syspermissionMapper;
    @Autowired
    SysrolepermissionMapper sysrolepermissionMapper;

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 员工列表展示 selectUserList
     **/
    public BaseRespose selectUserList(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();
        PageVo pageVo = null;
        try {
            pageVo = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), PageVo.class);
        } catch (Exception e) {
            throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL);
        }
        PageHelper.startPage(pageVo.getPagenum(), pageVo.getPagesize());
        PageHelper.orderBy("CreateDate desc");
        List<Sysuser> sysuserList = sysuserMapper.selectUserList(pageVo);
        PageInfo<Sysuser> pageInfo = new PageInfo<>(sysuserList);
        PageResposeVo pageResposeVo = new PageResposeVo(pageInfo.getTotal(), pageInfo.getList());
        baseRespose.setReturnString(pageResposeVo);

        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 添加员工 addUser
     **/

    public BaseRespose addUser(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();
        Sysuser sysuser = null;
        try {
            sysuser = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), Sysuser.class);
        } catch (Exception e) {
            throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL);
        }
        ValidParamUtils.validBusinessParam(sysuser, ossValidVo.getNull_addUser(), ossValidVo.getLenght_addUser());
        Sysuser sysuser1 = sysuserMapper.selectByLoginName(sysuser.getLoginname());
        if (!ObjectUtils.isEmpty(sysuser1)) {
            throw new ServiceException(203020, "登录名已存在", JSON.toJSONString(baseRequest));
        }
        Sysuser sysuserNow = cacheService.getSysUser(baseRequest.getUserId());
        sysuser.setPassword(Md5Util.psdMd5(sysuser.getPassword(), sysuser.getLoginname()));
        sysuser.setCreatedate(new Date());
        sysuser.setDeletemark(0);
        sysuser.setCreateuserid(sysuserNow.getUserid());
        sysuser.setCreateusername(sysuserNow.getName());
        sysuserMapper.insertSelective(sysuser);
        baseRespose.setReturnString(" ");

        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 修改员工信息 updateUserInfo
     **/

    public BaseRespose updateUserInfo(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();
        Sysuser sysuser = null;
        try {
            sysuser = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), Sysuser.class);
        } catch (Exception e) {
            throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL);
        }
        if (ObjectUtils.isEmpty(sysuser.getUserid())) {
            throw new ServiceException(203020, "用户id不能为空", JSON.toJSONString(baseRequest));
        }
        Sysuser sysuser1 = cacheService.getSysUser(sysuser.getUserid());
        if (ObjectUtils.isEmpty(sysuser1)) {
            throw new ServiceException(203020, "未找到该员工", JSON.toJSONString(baseRequest));
        }
        if (!ObjectUtils.isEmpty(sysuser.getPassword())) {
            sysuser.setPassword(Md5Util.psdMd5(sysuser.getPassword(), sysuser1.getLoginname()));
        }
        Sysuser sysuserNow = cacheService.getSysUser(baseRequest.getUserId());
        sysuser.setUpdatedete(new Date());
        sysuser.setUpdateuserid(sysuserNow.getUserid());
        sysuser.setUpdateusername(sysuserNow.getName());
        sysuserMapper.updateUserInfo(sysuser);
        sysuser = sysuserMapper.selectByPrimaryKey(sysuser.getUserid());
        MapConfig.userMap.put(sysuser.getUserid(), sysuser);
        baseRespose.setReturnString(" ");
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 删除员工 deleteUser
     **/
    public BaseRespose deleteUser(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();
        Sysuser sysuser = null;
        try {
            sysuser = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), Sysuser.class);
        } catch (Exception e) {
            throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL);
        }
        if (ObjectUtils.isEmpty(sysuser.getUserid())) {
            throw new ServiceException(203020, "用户id不能为空", JSON.toJSONString(baseRequest));
        }
        Sysuser sysuserNow = cacheService.getSysUser(baseRequest.getUserId());
        sysuser.setDeletemark(1);
        sysuser.setDeletedate(new Date());
        sysuser.setDeleteuserid(sysuserNow.getUserid());
        sysuser.setDeleteusername(sysuserNow.getName());
        sysuserMapper.updateByPrimaryKeySelective(sysuser);
        MapConfig.userMap.put(sysuser.getUserid(), null);
        baseRespose.setReturnString(" ");
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 员工修改密码 updateUserPsd
     **/
//    @UserLog(operateName = "员工修改密码", type = 3)
    public BaseRespose updateUserPsd(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();
        SysuserCustom sysuserCustom = null;
        try {
            sysuserCustom = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), SysuserCustom.class);
        } catch (Exception e) {
            throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL);
        }
        if (ObjectUtils.isEmpty(sysuserCustom.getNewpsd()) || ObjectUtils.isEmpty(sysuserCustom.getOldpsd())) {
            throw new ServiceException(203020, "请求信息不完整", JSON.toJSONString(baseRequest));
        }
        if (sysuserCustom.getNewpsd().equals(sysuserCustom.getOldpsd())) {
            throw new ServiceException(203020, "新密码不能跟旧密码一样", JSON.toJSONString(baseRequest));
        }
        Sysuser sysuser = cacheService.getSysUser(baseRequest.getUserId());
        System.out.println(JSON.toJSONString(sysuser));
        if (!Md5Util.psdMd5(sysuserCustom.getOldpsd(), sysuser.getLoginname()).equals(sysuser.getPassword())) {
            throw new ServiceException(203020, "密码错误", JSON.toJSONString(baseRequest));
        }
        Sysuser sysuser1 = new Sysuser();
        sysuser1.setUserid(baseRequest.getUserId());
        sysuser1.setPassword(Md5Util.psdMd5(sysuserCustom.getNewpsd(), sysuser.getLoginname()));
        sysuserMapper.updateByPrimaryKeySelective(sysuser1);
        sysuser.setPassword(sysuser1.getPassword());
        MapConfig.userMap.put(sysuser.getUserid(), sysuser);
        baseRespose.setReturnString(" ");
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 角色列表展示 selectRoleList
     **/
    public BaseRespose selectRoleList(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();
        PageVo pageVo = null;
        try {
            pageVo = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), PageVo.class);
        } catch (Exception e) {
            throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL);
        }
        PageHelper.startPage(pageVo.getPagenum(), pageVo.getPagesize());
        PageHelper.orderBy("roleid desc");
        List<Sysrole> sysroleList = sysroleMapper.selectByExample(null);
        PageInfo<Sysrole> pageInfo = new PageInfo<>(sysroleList);
        PageResposeVo pageResposeVo = new PageResposeVo(pageInfo.getTotal(), pageInfo.getList());
        baseRespose.setReturnString(pageResposeVo);
        return baseRespose;
    }
    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 添加角色 addRole
     **/
    public BaseRespose addRole(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();
        Sysrole sysrole = null;
        try {
            sysrole = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), Sysrole.class);
        } catch (Exception e) {
            throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL);
        }
        if (ObjectUtils.isEmpty(sysrole.getRole())) {
            throw new ServiceException(203020, "角色名不能为空", JSON.toJSONString(baseRequest));
        }
        SysroleExample example = new SysroleExample();
        example.createCriteria().andRoleEqualTo(sysrole.getRole());
        List<Sysrole> sysroleList = sysroleMapper.selectByExample(example);
        if (sysroleList.size() > 0) {
            throw new ServiceException(203020, "角色名已存在", JSON.toJSONString(baseRequest));
        }
        sysroleMapper.insertSelective(sysrole);
        baseRespose.setReturnString(" ");
        return baseRespose;
    }
    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 修改角色 updateRole
     **/
    public BaseRespose updateRole(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();
        Sysrole sysrole = null;
        try {
            sysrole = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), Sysrole.class);
        } catch (Exception e) {
            throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL);
        }
        if (ObjectUtils.isEmpty(sysrole.getRoleid())) {
            throw new ServiceException(203020, "角色id不能为空", JSON.toJSONString(baseRequest));
        }
        if (!ObjectUtils.isEmpty(sysrole.getRole())) {
            Sysrole sysrole1 = sysroleMapper.selectByPrimaryKey(sysrole.getRoleid());
            SysroleExample example = new SysroleExample();
            example.createCriteria().andRoleEqualTo(sysrole.getRole()).andRoleNotEqualTo(sysrole1.getRole());
            List<Sysrole> sysroleList = sysroleMapper.selectByExample(example);
            if (sysroleList.size() > 0) {
                throw new ServiceException(203020, "角色名已存在", JSON.toJSONString(baseRequest));
            }
        }
        sysroleMapper.updateByPrimaryKeySelective(sysrole);
        baseRespose.setReturnString(" ");
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 删除角色 deleteRole
     **/
    public BaseRespose deleteRole(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();
        Sysrole sysrole = null;
        try {
            sysrole = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), Sysrole.class);
        } catch (Exception e) {
            throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL);
        }
        sysroleMapper.deleteByPrimaryKey(sysrole.getRoleid());
        SysuserroleExample example = new SysuserroleExample();
        example.createCriteria().andRoleidEqualTo(sysrole.getRoleid());
        List<Sysuserrole> sysuserroleList = sysuserroleMapper.selectByExample(example);
        sysuserroleMapper.deleteByExample(example);
        for (Sysuserrole role : sysuserroleList
                ) {
            cacheService.setUserPer(role.getUserid());
        }
        baseRespose.setReturnString(" ");

        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 查询员工角色 selectUserRole
     **/
    public BaseRespose selectUserRole(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();
        Sysuser sysuser = null;
        try {
            sysuser = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), Sysuser.class);
        } catch (Exception e) {
            throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL);
        }
        if (ObjectUtils.isEmpty(sysuser.getUserid())) {
            throw new ServiceException(203020, "用户id不能为空", JSON.toJSONString(baseRequest));
        }
        List<SysroleCustom> sysroleCustomList = sysroleMapper.selectUserRole(sysuser.getUserid());
        baseRespose.setReturnString(sysroleCustomList);

        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 设置员工角色 updateUserRole
     **/

    public BaseRespose updateUserRole(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();

        SysroleCustom sysroleCustom = null;
        try {
            sysroleCustom = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), SysroleCustom.class);
        } catch (Exception e) {
            throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL);
        }
        if (ObjectUtils.isEmpty(sysroleCustom.getUserid())) {
            throw new ServiceException(203020, "用户Id不能为空", JSON.toJSONString(baseRequest));
        }
        SysuserroleExample example = new SysuserroleExample();
        example.createCriteria().andUseridEqualTo(sysroleCustom.getUserid());
        sysuserroleMapper.deleteByExample(example);

        if (!ObjectUtils.isEmpty(sysroleCustom.getRoleidlist())) {
            Sysuserrole sysuserrole = new Sysuserrole();
            sysuserrole.setUserid(sysroleCustom.getUserid());
            for (Integer roleid : sysroleCustom.getRoleidlist()
                    ) {
                sysuserrole.setRoleid(roleid);
                sysuserroleMapper.insertSelective(sysuserrole);
            }
        }
        cacheService.setUserPer(sysroleCustom.getUserid());
        baseRespose.setReturnString(" ");

        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 查询角色权限 selectRolePer
     **/
    public BaseRespose selectRolePer(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();
        Sysrole sysrole = null;
        try {
            sysrole = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), Sysrole.class);
        } catch (Exception e) {
            throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL);
        }
        if (ObjectUtils.isEmpty(sysrole.getRoleid())) {
            throw new ServiceException(203020, "角色id不能为空", JSON.toJSONString(baseRequest));
        }
        List<TreeNode> treeNodeList = syspermissionMapper.selectRolePer(sysrole.getRoleid());
        treeNodeList = TreeUtils.listToTree(treeNodeList);
        baseRespose.setReturnString(treeNodeList);
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 设置角色权限 updateUserPer
     **/
    public BaseRespose updateUserPer(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();
        SyspermissionCustom syspermissionCustom = null;
        try {
            syspermissionCustom = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), SyspermissionCustom.class);
        } catch (Exception e) {
            throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL);
        }
        if (ObjectUtils.isEmpty(syspermissionCustom.getRoleid())) {
            throw new ServiceException(203020, "角色Id不能为空", JSON.toJSONString(baseRequest));
        }
        if (ObjectUtils.isEmpty(syspermissionCustom.getPeridlist())) {
            SysrolepermissionExample example = new SysrolepermissionExample();
            example.createCriteria().andRoleidEqualTo(syspermissionCustom.getRoleid());
            sysrolepermissionMapper.deleteByExample(example);
        } else {

            if (syspermissionCustom.getPeridlist().size() == 0) {
                SysrolepermissionExample example = new SysrolepermissionExample();
                example.createCriteria().andRoleidEqualTo(syspermissionCustom.getRoleid());
                sysrolepermissionMapper.deleteByExample(example);
            } else {
//                List<Integer> perlist = syspermissionMapper.selectPerListForList(syspermissionCustom.getPeridlist());
//                syspermissionCustom.setPeridlist(perlist);
                //删除不在新的权限中的权限
                SysrolepermissionExample example = new SysrolepermissionExample();
                example.createCriteria().andRoleidEqualTo(syspermissionCustom.getRoleid()).andPermissionidNotIn(syspermissionCustom.getPeridlist());
                sysrolepermissionMapper.deleteByExample(example);
                //查询角色还剩的权限
                Set<Integer> set = syspermissionMapper.slectIdByRoleid(syspermissionCustom.getRoleid());
                Sysrolepermission sysrolepermission = new Sysrolepermission();
                sysrolepermission.setRoleid(syspermissionCustom.getRoleid());
                //插入新的
                for (Integer perid : syspermissionCustom.getPeridlist()
                        ) {
                    if (!set.contains(perid)) {
                        sysrolepermission.setPermissionid(perid);
                        sysrolepermissionMapper.insertSelective(sysrolepermission);
                    }
                }
            }
        }
        SysuserroleExample example = new SysuserroleExample();
        example.createCriteria().andRoleidEqualTo(syspermissionCustom.getRoleid());
        List<Sysuserrole> sysuserroleList = sysuserroleMapper.selectByExample(example);
        for (Sysuserrole role : sysuserroleList
                ) {
            cacheService.setUserPer(role.getUserid());
        }
        baseRespose.setReturnString(" ");
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 查询权限列表 selectPerList
     **/
    public BaseRespose selectPerList(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();
        Sysuser sysuser = cacheService.getSysUser(baseRequest.getUserId());
        if (sysuser.getUsertype() != 3) {
            throw new ServiceException(203020, "只有超管可操作", JSON.toJSONString(baseRequest));
        }
        List<TreeNode> treeNodeList = syspermissionMapper.selectPerList();
        treeNodeList = TreeUtils.listToTree(treeNodeList);
        baseRespose.setReturnString(treeNodeList);

        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 查询权限详情 selectPerInfo
     **/
    public BaseRespose selectPerInfo(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();

        Syspermission syspermission = null;
        try {
            syspermission = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), Syspermission.class);
        } catch (Exception e) {
            throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL);
        }
        Sysuser sysuser = cacheService.getSysUser(baseRequest.getUserId());
        if (sysuser.getUsertype() != 3) {
            throw new ServiceException(203020, "只有超管可操作", JSON.toJSONString(baseRequest));
        }
        syspermission = syspermissionMapper.selectByPrimaryKey(syspermission.getPermissionid());
        baseRespose.setReturnString(syspermission);
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 添加权限 addPer
     **/
    public BaseRespose addPer(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();
        Syspermission syspermission = null;
        try {
            syspermission = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), Syspermission.class);
        } catch (Exception e) {
            throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL);
        }
        Sysuser sysuser = cacheService.getSysUser(baseRequest.getUserId());
        if (sysuser.getUsertype() != 3) {
            throw new ServiceException(203020, "只有超管可操作", JSON.toJSONString(baseRequest));
        }
        if (ObjectUtils.isEmpty(syspermission.getParentid()) || ObjectUtils.isEmpty(syspermission.getPermissionname()) || ObjectUtils.isEmpty(syspermission.getSort())) {
            throw new ServiceException(203020, "请求信息不完整", JSON.toJSONString(baseRequest));
        }
        if (syspermission.getResourcetype() == 2) {
            if (ObjectUtils.isEmpty(syspermission.getPermission())) {
                throw new ServiceException(203020, "功能权限的权限吗不能为空", JSON.toJSONString(baseRequest));
            }
        }
        syspermissionMapper.insertSelective(syspermission);
        baseRespose.setReturnString(" ");
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 修改权限 updatePer
     **/
    public BaseRespose updatePer(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();
        Syspermission syspermission = null;
        try {
            syspermission = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), Syspermission.class);
        } catch (Exception e) {
            throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL);
        }
        Sysuser sysuser = cacheService.getSysUser(baseRequest.getUserId());
        if (sysuser.getUsertype() != 3) {
            throw new ServiceException(203020, "只有超管可操作", JSON.toJSONString(baseRequest));
        }
        syspermissionMapper.updateByPrimaryKeySelective(syspermission);
        baseRespose.setReturnString(" ");

        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 删除权限 deletePer
     **/
    public BaseRespose deletePer(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();
        Syspermission syspermission = null;
        try {
            syspermission = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), Syspermission.class);
        } catch (Exception e) {
            throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL);
        }
        Sysuser sysuser = cacheService.getSysUser(baseRequest.getUserId());
        if (sysuser.getUsertype() != 3) {
            throw new ServiceException(203020, "只有超管可操作", JSON.toJSONString(baseRequest));
        }
        if (ObjectUtils.isEmpty(syspermission.getPermissionid())) {
            throw new ServiceException(203020, "权限id不能为空", JSON.toJSONString(baseRequest));
        }
        if (syspermission.getPermissionid() == 0) {
            throw new ServiceException(203020, "权限id错误", JSON.toJSONString(baseRequest));
        }
        Syspermission syspermission1 = syspermissionMapper.selectByPrimaryKey(syspermission.getPermissionid());
        if (ObjectUtils.isEmpty(syspermission1)) {
            throw new ServiceException(203020, "未找到该权限", JSON.toJSONString(baseRequest));
        }
        if (syspermission1.getResourcetype() == 1) {
            SyspermissionExample example = new SyspermissionExample();
            example.createCriteria().andParentidEqualTo(syspermission.getPermissionid());
            //可能存在二级菜单
            List<Syspermission> syspermissionList = syspermissionMapper.selectByExample(example);
            syspermissionMapper.deleteByExample(example);
            for (Syspermission per : syspermissionList
                    ) {
                if (per.getResourcetype() == 1) {
                    example = new SyspermissionExample();
                    example.createCriteria().andParentidEqualTo(per.getPermissionid());
                    syspermissionMapper.deleteByExample(example);
                }
            }
        }
        syspermissionMapper.deleteByPrimaryKey(syspermission.getPermissionid());
        baseRespose.setReturnString(" ");
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 员工个人中心 selectStaffInfo
     **/
    public BaseRespose selectStaffInfo(BaseRequest baseRequest) {
        BaseRespose baseRespose = new BaseRespose();
        Sysuser sysuser = null;
        try {
            sysuser = JSON.parseObject(JSON.toJSONString(baseRequest.getRequestString()), Sysuser.class);
        } catch (Exception e) {
            throw new ServiceException(ServiceCodeEnum.INFO_IS_FAIL);
        }
        if (ObjectUtils.isEmpty(sysuser.getUserid())) {
            throw new ServiceException(203020, "请求信息不完整", JSON.toJSONString(baseRequest));
        }
        sysuser = cacheService.getSysUser(sysuser.getUserid());
        sysuser.setPassword(null);
        baseRespose.setReturnString(sysuser);
        return baseRespose;
    }


    private double ExchangeVal(double index) {
        double val = -1;

        if (index <= 16094) {
            val = Math.round(1.80800487602553E-83 * (Math.pow(index, 22)) - 3.41713387254528E-78 * (Math.pow(index, 21))
                    + 3.02974381672949E-73 * (Math.pow(index, 20)) - 1.67868950891817E-68 * (Math.pow(index, 19))
                    + 6.52911506352817E-64 * (Math.pow(index, 18)) - 1.89688932403074E-59 * (Math.pow(index, 17))
                    + 4.27244528643799E-55 * (Math.pow(index, 16)) - 7.63365946576145E-51 * (Math.pow(index, 15))
                    + 1.09684639522812E-46 * (Math.pow(index, 14)) - 1.27628304586939E-42 * (Math.pow(index, 13))
                    + 1.20490743537359E-38 * (Math.pow(index, 12)) - 9.20910255907167E-35 * (Math.pow(index, 11))
                    + 5.66640000975886E-31 * (Math.pow(index, 10)) - 2.78267960167693E-27 * (Math.pow(index, 9))
                    + 1.07806242302442E-23 * (Math.pow(index, 8)) - 3.2472699232534E-20 * (Math.pow(index, 7))
                    + 7.47341595335605E-17 * (Math.pow(index, 6)) - 1.28928266808362E-13 * (Math.pow(index, 5))
                    + 1.64280601164035E-10 * (Math.pow(index, 4)) - 1.58222706393464E-07 * (Math.pow(index, 3))
                    + 0.000195902811461781 * (Math.pow(index, 2)) + 0.0309720378566637 * (Math.pow(index, 1))
                    - 0.300045135425303);
        } else if (index >= 160000) {
            val = -1;
        } else {
            val = Math.round(1.22915678840658E-54 * (Math.pow(index, 12)) - 9.21771055741185E-49 * (Math.pow(index, 11))
                    + 3.08780670794864E-43 * (Math.pow(index, 10)) - 6.10007592155746E-38 * (Math.pow(index, 9))
                    + 7.90463755507625E-33 * (Math.pow(index, 8)) - 7.07394474592792E-28 * (Math.pow(index, 7))
                    + 4.48883773027515E-23 * (Math.pow(index, 6)) - 2.04791480250671E-18 * (Math.pow(index, 5))
                    + 6.80811446999673E-14 * (Math.pow(index, 4)) - 1.74128460294331E-09 * (Math.pow(index, 3))
                    + 0.000071238711150178 * (Math.pow(index, 2)) + 0.119482875082178 * (Math.pow(index, 1))
                    + 7.28420650882197);
        }

        return val;
    }

    public static void main(String[] args) {

                System.out.println( Md5Util.psdMd5("1234567", "liuhao"));

    }
}
